package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyLike;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyLikeMapper;
import cn.wolfcode.trip.base.query.LikeQueryObject;
import cn.wolfcode.trip.base.service.IStrategyLikeService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyLikeServiceImpl implements IStrategyLikeService {
    @Autowired
    private StrategyLikeMapper strategyLikeMapper;


    public void save(StrategyLike strategyLike) {
        //设置点赞的用户
        strategyLike.setUser(UserContext.getUser());
        // 创建点赞的时间
        strategyLike.setLikeTime(new Date());
        strategyLikeMapper.insert(strategyLike);
    }

    public Boolean getUserForLike(Long strategyId) {
        User user = UserContext.getUser();
        List<Long> userIdList=strategyLikeMapper.selectAllUserId(strategyId);
        if (userIdList.contains(user.getId())){
            return true;
        }
        return false;
    }

    public void deleteUserForLike(Long strategyId) {
        User user = UserContext.getUser();
        strategyLikeMapper.deleteByUserIdAndStrategyId(strategyId,user.getId());
    }

    public void saveCommentLike(StrategyLike strategyLike) {
        //设置点赞的用户
        strategyLike.setUser(UserContext.getUser());
        // 创建点赞的时间
        strategyLike.setLikeTime(new Date());
        strategyLike.setState(1);
        strategyLikeMapper.insert(strategyLike);
    }

    public void deleteCommentForLike(Long commentId) {
        User user = UserContext.getUser();
        strategyLikeMapper.deleteByUserIdAndCommentId(commentId,user.getId());
    }

    public Boolean getCommentForLike(Long commentId) {
        User user = UserContext.getUser();
        List<Long> userIdList=strategyLikeMapper.selectAllCommentUserId(commentId);
        if (userIdList.contains(user.getId())){
            return true;
        }
        return false;
    }

    public int countForLikeMe(Long userId) {
        return strategyLikeMapper.countForLikeMe(userId);
    }

    public PageInfo<StrategyLike> selectListForLikeUser(LikeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyLike> list = strategyLikeMapper.ListForLikeComment(qo);
        return new PageInfo(list);
    }

    public int countForCommentMe(Long userId) {
        return strategyLikeMapper.countForCommentMe(userId);
    }
}
